package cn.yunyichina.provider.drug.service.impl;

import cn.yunyichina.provider.drug.vo.SettingVo;
import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.framework.annotation.validate.Validator;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.base.Org;
import cn.yunyichina.provider.iface.entity.base.Rule;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 系统设置服务
 * Created by Administrator on 2017/8/3.
 */
@Service
public class SettingServiceImpl {
    private Logger logger = LoggerFactory.getLogger(SettingServiceImpl.class);

    /**
     * 保存参数配置
     * yyt.drug.setting.save
     * @param data
     * @param sessionId
     * @return
     */
    public Response saveSetting(String data, String sessionId) {
        logger.info("保存参数配置--->method:saveSetting,data:" + data);
        Response response = new Response();
        SettingVo vo = JsonUtils.parseObject(data,SettingVo.class);

        // 校验RequestVo的数据
        try {
            Validator.getInstance().validate(vo);
        } catch (ValidateException ex) {
            logger.info(" 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            logger.error("error", ex);
            response.setResultCodeAndMessage("23001", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }
        //-----------------------------------------------------------------------------------------------------
        Rule rule = new Rule();
        rule.setAttributeType((short) 6);
        rule.setOrgCode(vo.getOrgCode());

        if(StringUtils.isNotBlank(vo.getDrugAutoDealType())){
            rule.setAttributeName("drugAutoDealType");
            rule.setInputType((short) 2);
            rule.setAttributeProperties(vo.getDrugAutoDealType());
            rule.setAttributeDesc("药店订单超时自动处理类型");
            Response ruleResp = BaseService.callHumpFromInternal("yyt.base.rule.save",JsonUtils.toJsonString(rule));
            if(!ruleResp.getResultCode().equals("0")){
                return ruleResp;
            }
        }
        if(StringUtils.isNotBlank(vo.getNewOrderNotifyType())){
            rule.setAttributeName("newOrderNotifyType");
            rule.setInputType((short) 3);
            rule.setAttributeProperties(vo.getNewOrderNotifyType());
            rule.setAttributeDesc("处方外购新订单提醒方式");
            Response ruleResp = BaseService.callHumpFromInternal(sessionId,"yyt.base.rule.save",JsonUtils.toJsonString(rule));
            if(!ruleResp.getResultCode().equals("0")){
                return ruleResp;
            }
        }

        //保存手机号
        if(vo.getNewOrderNotifyType()!= null && vo.getNewOrderNotifyType().indexOf("2") >= 0){
            Org org = BaseService.getBaseClient().getOrgByCode(vo.getOrgCode());
            if (org == null) {
                response.setResultCodeAndMessage("23900", "没有org_code对应的机构信息:" + vo.getOrgCode() + "！");
                return response;
            }
            org.setContactTel(vo.getMobile());
            org.setUpdateTime(DateUtils.dateToString(new Date()));
            Response orgResp =  BaseService.callHumpFromInternal(sessionId,"yyt.base.org.save",JsonUtils.toJsonString(org));
            logger.info("更新联系电话,response= " + JsonUtils.toJsonString(orgResp));
        }

        response.setResultCodeAndMessage("0","保存成功");
        return response;
    }

    /**
     * 参数配置查询
     * yyt.drug.setting.get
     * @param data
     * @param sessionId
     * @return
     */
    public Response getSetting(String data, String sessionId) {
        logger.info("参数配置查询--->method:getSetting,data:" + data);
        Response response = new Response();
        SettingVo vo = JsonUtils.parseObject(data,SettingVo.class);

        // 校验RequestVo的数据
        if(StringUtils.isBlank(vo.getOrgCode())){
            response.setResultCodeAndMessage("23001", "org_code不能为空");
            return response;
        }
        String drugAutoDealType = "2";
        String newOrderNotifyType = "1";
        String drugRefundApplyUnprocess = "2";

        String ruleQuery = "{\"attribute_name\":\"drugAutoDealType\",\"org_code\":\"" + vo.getOrgCode() +"\"}";
        Response ruleResp = BaseService.callHumpFromInternal("yyt.base.rule.select",ruleQuery);
        if(ruleResp!=null && ruleResp.getResultCode().equals("0")){
            Rule rule = JsonUtils.parseObject(ruleResp.getResult(),Rule.class);
            if(rule != null){
                drugAutoDealType = rule.getAttributeProperties();
            }
        }

        ruleQuery = "{\"attribute_name\":\"newOrderNotifyType\",\"org_code\":\"" + vo.getOrgCode() +"\"}";
        ruleResp = BaseService.callHumpFromInternal("yyt.base.rule.select",ruleQuery);
        if(ruleResp!=null && ruleResp.getResultCode().equals("0")){
            Rule rule = JsonUtils.parseObject(ruleResp.getResult(),Rule.class);
            if(rule != null){
                newOrderNotifyType = rule.getAttributeProperties();
            }
        }

        ruleQuery = "{\"attribute_name\":\"drugRefundApplyUnprocess\",\"org_code\":\"" + vo.getOrgCode() +"\"}";
        ruleResp = BaseService.callHumpFromInternal("yyt.base.rule.select",ruleQuery);
        if(ruleResp!=null && ruleResp.getResultCode().equals("0")){
            Rule rule = JsonUtils.parseObject(ruleResp.getResult(),Rule.class);
            if(rule != null){
                drugRefundApplyUnprocess = rule.getAttributeProperties();
            }
        }

        vo.setDrugAutoDealType(drugAutoDealType);
        vo.setNewOrderNotifyType(newOrderNotifyType);
        vo.setDrugRefundApplyUnprocess(drugRefundApplyUnprocess);

        //保存手机号
        if(newOrderNotifyType.indexOf("2") >= 0 ){
            Org org = BaseService.getBaseClient().getOrgByCode(vo.getOrgCode());
            if (org == null) {
                response.setResultCodeAndMessage("23900", "没有org_code对应的机构信息:" + vo.getOrgCode() + "！");
                return response;
            }
            vo.setMobile(org.getContactTel());
        }

        response.setResultCodeAndMessage("0","查询成功");
        response.setResult(JsonUtils.toJsonString(vo));
        return response;
    }
}
